//
//  StatusCell.swift
//  0110-weibo
//
//  Created by 鲍志刚 on 16/1/16.
//  Copyright © 2016年 zhongye. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    //定义底部视图的顶部约束的属性
    var bottomViewTopConstraints: Constraint?
    
    var status: Status? {
        didSet{
            //绑定数据
            //设置原创微博的数据模型
            statusOriginalView.status = status
            //1.卸载约束
            self.bottomViewTopConstraints?.uninstall()
            
            if let re = status?.retweeted_status {
            
                //转发微博
                
                //1.设置数据源
                statusCellRetweedView.retweeted_status = re
                
                //2.显示转发微博视图
                statusCellRetweedView.hidden = false
                
                //3.更新约束 相对于转发微博的底部 + 10
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(statusCellRetweedView.snp_bottom).constraint
                })
                
            } else {
                //1.隐藏转发微博视图
                statusCellRetweedView.hidden = true
                //2.更新约束 相对原创微博的底部 + 10
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
                
                
            }
  
            
        }
    
    
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        
        setupUI()
    }
    
    //设置ui
    func setupUI(){

//        self.backgroundColor = UIColor.yellowColor()
        
    //添加子控件
    contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusCellRetweedView)
    contentView.addSubview(statusCellBottomView)
        
        
        //设置约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(contentView)
            //测试高度
//            make.height.equalTo(80)
        }
        
        // 需要更改 底部的toolbar的约束
        statusCellRetweedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.equalTo(statusOriginalView.snp_left)
            make.right.equalTo(statusOriginalView.snp_right)
        }
        
        
        statusCellBottomView.snp_makeConstraints { (make) -> Void in
//            make.bottom.left.right.equalTo(contentView)

              self.bottomViewTopConstraints = make.top.equalTo(statusCellRetweedView.snp_bottom).constraint
//            make.top.equalTo(statusCellRetweedView.snp_bottom)
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        
        //给contentView设置约束条件
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            
//            自行计算行高的最关键的一条约束
            make.bottom.equalTo(statusCellBottomView.snp_bottom)
        }
        
        
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //如果从 xib / sb 加载  会调用到这个方法
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    //懒加载子视图 原创微博
    private lazy var  statusOriginalView : StatusOriginalView = StatusOriginalView()
    private lazy var statusCellBottomView : StatusCellBottomView = StatusCellBottomView()
    
    private lazy var statusCellRetweedView: StatusCellRetweetedView = StatusCellRetweetedView()
    


}
